<?

//Data loading
$title='DAFTAR PELUNASAN HUTANG';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI TATANAN CONTAINER';

$page   = 'PAGE';

$DEFINE_FONT=0;
$Result = GridList ("", "", "");
if ($Result) {
	foreach ($Result as $Row) {
		foreach ($Row as $Field) {
			if (strlen($strExcelRow)) $strExcelRow .= ";$Field";
			else $strExcelRow = "$Field";
		}
		$strOutput .= $strExcelRow."\n";
		$strExcelRow="";
	}
	header("Content-type:application/vnd.notepad");
	header("Content-Disposition:filename=$filename".".csv");
	print $strOutput;
}

function GridList () {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    $fromDate = GetParam("fromDate","");
    $toDate = GetParam("toDate","");
    list($FDay,$FMonth,$FYear) = explode("/",$fromDate);
    list($TDay,$TMonth,$TYear) = explode("/",$toDate);
	$SQL = "SELECT *, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate FROM ais_hutang WHERE srt=2 ";
	if (GetParam("office",""))
	$SQL .= " AND ktrasl='".GetParam("office","")."'";

	if (GetParam("fromDate",""))
	$SQL .= " AND tglbkt>='$FYear-$FMonth-$FDay'";
	if (GetParam("toDate",""))
	$SQL .= " AND tglbkt<='$TYear-$TMonth-$TDay'";	
	
	$SQL .= " ORDER BY debnam";
//	print $SQL."<br><br>\n\n\n\n";
	$MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
} 


class GridPDFReport extends PDF {

	function Header() {
		global $title,$perush,$page,$DEFINE_FONT;
		if ($DEFINE_FONT==0) {
			$this->AddFont('arialn','','arialn.php');
			$DEFINE_FONT=1;
		}
		//$this->SetLeftMargin(5);
		$this->SetFont('arialn','',10);
    	$this->Cell(200,5,$perush,0,1,'C',0);
    	$this->Cell(200,5,$title,0,1,'C',0);
    	$this->Cell(200,5,"PERIOD: ".GetParam("fromDate","")."  -  ".GetParam("toDate",""),0,1,'C',0);
		$this->SetLeftMargin(5);
    	$this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
    	$this->Line(5,30,205,30);
    	$this->Ln();
    	$this->Ln();
	    //Header
	    //Column widths

	    $w=array(60,25,20,70,25);
	    //$this->SetFont('arialn','',10);
	    $this->Cell($w[0],7,"DEBTUR",1,0,'C',0);
	    $this->Cell($w[1],7,"NO. BUKTI",1,0,'C',0);	    
	    $this->Cell($w[2],7,"DATE",1,0,'C',0);
	    $this->Cell($w[3],7,"URAIAN",1,0,'C',0);
	    $this->Cell($w[4],7,"JUMLAH",1,0,'C',0);
	    $this->Ln();
	    $this->SetFont('arialn','',8);
	}

	function GridTable($Result) {
	    //Column widths
	    $w=array(60,25,20,70,25);
	    //Data
	    foreach($Result as $row)
	    {
		    if ($row["dk"]=="D") $row["debet"] = $row["jumlah"];
			else $row["credit"] = $row["jumlah"];
			$total = $total + $row["jumlah"];

	        if (($lastdebnam<>$row["debnam"]) && (strlen($lastdebnam)>0)) {
		        $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"SUB TOTAL:",1,0,'R',0);
		        $this->Cell($w[4],7,number_format($subtotal,2),1,0,'R',0);
		        $this->Ln();
	        	$subtotal = 0;
        	}
	        $subtotal = $subtotal + $row["jumlah"];

			if (($lastdebnam<>$row["debnam"]) && (strlen($row["debnam"])))
	        	$this->Cell($w[0],6,$row["debnam"],'LR');
	        else
	         	$this->Cell($w[0],6,"",'LR');

	        $this->Cell($w[1],6,$row["nobkt"],'LR');	        
	        $this->Cell($w[2],6,$row["trdate"],'LR',0,'C');
	        $this->Cell($w[3],6,"PAID ".$row["nobkta"],'LR');
	        $this->Cell($w[4],6,number_format($row["jumlah"],2),'LR',0,'R');
	        $this->Ln();
	        $lastdebnam = $row["debnam"];	        
	    }
		$this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"SUB TOTAL:",1,0,'R',0);
		$this->Cell($w[4],7,number_format($subtotal,2),1,0,'R',0);
		$this->Ln();
	    
	    $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"JUMLAH: ",1,0,'R',0);
	    $this->Cell($w[4],7,number_format($total,2),1,0,'R',0);
	    $this->Ln();
	    
	    //Closure line
	    //$this->Cell(array_sum($w),0,'','T');
	}

	function Footer() {
	    //Closure line
	    $this->Cell(200,0,'','T');
	}

}


?>